<?php
include_once 'util/arrayutil.php';

class Genre{
    var $id;
    var $name;
    var $description;
    
    
    function __toString() {
        return $this->name;
    }
    
    function __construct($_id,$_name,$_description) {
        $this->id = $_id;
        $this->name =$_name;
        $this->description =$_description;        
    }
    
    function setId($_id){
        $this->id = $_id;
    }
    
    function getId(){
        return $this->id;
    }
    
    function setName($_name){
        $this->name = $_name;
    }
    
    function getName(){
        return $this->name;
    }
    
    function setDescription($_description){
        $this->description = $_description;
    }
    
    function getDescription(){
        return $this->description;
    }
    
    function save(){        
        mysql_query("insert into genre(genre_name,description) 
                values('$this->name','$this->description')");
    }
    
    function delete(){
        mysql_query("delete from genre where id=$this->id");
    }
    
    function update(){
        mysql_query("update genre set name='$this->name', description='$this->description' 
                where id=$this->id");
    }
    
    static function  load($id){
        $result = mysql_query("select id,genre_name,description from genre where id=$id")
        or die(mysql_error());
        $row = mysql_fetch_row($result);
        $genre = new Genre($row[0],$row[1],$row[2]);
        return $genre;
    }
    
    public static function loads(){
        $result = mysql_query("select id,genre_name,description from genre");
        $genres = array();
        while($row = mysql_fetch_array($result)){
            $genre = new Genre($row['id'],$row['genre_name'],$row['description']);
            array_push($genres, $genre);
        }
        return $genres;
    }
    
    public static function loadsForJSON(){
        return json_encode(ArrayUtil::arrayOfObjectToArray(Genre::loads()));
    }
    
    public static function loadForJSON($id){
        return json_encode(ArrayUtil::arrayOfObjectToArray(Genre::load($id)));
    }
}

?>
